import 'package:base_library/base_library.dart';
import 'package:wan_android_flutter/protocol/model.dart';
import 'package:wan_android_flutter/repository/apis.dart';
import 'package:wan_android_flutter/repository/common.dart';

class UserRepository {
  Future<UserModel> login(LoginReq req) async {
    BaseRespR<Map<String, dynamic>> baseRespR = await DioUtil()
        .requestR<Map<String, dynamic>>(Method.post, WanAndroidApi.user_login,
            data: req.toJson());
    if (baseRespR.code != Constant.status_success) {
      return Future.error(baseRespR.msg);
    }
    baseRespR.response.headers.forEach((String name, List<String> values) {
      if (name == "set-cookie") {
        String cookie = values.toString();
        LogUtil.e("set-cookie: " + cookie);
        SpUtil.putString(BaseConstant.keyAppToken, cookie);
        DioUtil().setCookie(cookie);
        //CacheUtil().setLogin(true);
      }
    });
    UserModel userModel = UserModel.fromJson(baseRespR.data);
    SpUtil.putObject(BaseConstant.keyUserModel, userModel);

    return userModel;
  }
}
